<?php


/*
 * 取得优惠活动列表
 * @return   array
 */
function get_sql_favourable_list(){
	/* 过滤条件 */
	$filter['keyword']    	= 	empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);
	if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1){
		$filter['keyword'] 	= 	$filter['keyword'];
	}
	$filter['is_going']   	= 	empty($_REQUEST['is_going']) ? 0 : 1;
	$filter['sort_by']    	= 	empty($_REQUEST['sort_by']) ? 'act_id' : trim($_REQUEST['sort_by']);
	$filter['sort_order'] 	= 	empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);

	$where = "";
	if (!empty($filter['keyword'])){
		$where 				.= 	" AND act_name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'";
	}
	if ($filter['is_going']){
		$now 				= 	gmtime();
		$where 				.= 	" AND start_time <= '$now' AND end_time >= '$now' ";
	}

	/* 查询 */
	$sql 					= 	"SELECT * ".
								"FROM " . 'ybr_favourable_activity' .
								" WHERE 1 $where ".
								" ORDER BY $filter[sort_by] $filter[sort_order] ";
    return $sql;
}

/**
 * 根据过滤条件获得排序的标记
 *
 * @access  public
 * @param   array   $filter
 * @return  array
 */
function sort_flag($filter){
    $flag['tag']    = 'sort_' . preg_replace('/^.*\./', '', $filter['sort_by']);
    $flag['img']    = '<img src="' . __PUBLIC__ . '/Image/Admin/' . ($filter['sort_order'] == "DESC" ? 'sort_desc.gif' : 'sort_asc.gif') . '"/>';
    return $flag;
}

/**
 * 截取UTF-8编码下字符串的函数
 *
 * @param   string      $str        被截取的字符串
 * @param   int         $length     截取的长度
 * @param   bool        $append     是否附加省略号
 *
 * @return  string
 */
function sub_str($str, $length = 0, $append = true){
    $str = trim($str);
    $strlength = strlen($str);

    if ($length == 0 || $length >= $strlength){
        return $str;
    }
    elseif ($length < 0){
        $length = $strlength + $length;
        if ($length < 0){
            $length = $strlength;
        }
    }

    if (function_exists('mb_substr')){
        $newstr = mb_substr($str, 0, $length, EC_CHARSET);
    }
    elseif (function_exists('iconv_substr')){
        $newstr = iconv_substr($str, 0, $length, EC_CHARSET);
    }
    else{
        //$newstr = trim_right(substr($str, 0, $length));
        $newstr = substr($str, 0, $length);
    }

    if ($append && $str != $newstr){
        $newstr .= '...';
    }

    return $newstr;
}

/**
 *  将一个用户自定义时区的日期转为GMT时间戳
 *
 * @access  public
 * @param   string      $str
 *
 * @return  integer
 */
function local_strtotime($str){
    //$timezone = isset($_SESSION['timezone']) ? $_SESSION['timezone'] : $GLOBALS['_CFG']['timezone'];
    /**
    * $time = mktime($hour, $minute, $second, $month, $day, $year) - date('Z') + (date('Z') - $timezone * 3600)
    * 先用mktime生成时间戳，再减去date('Z')转换为GMT时间，然后修正为用户自定义时间。以下是化简后结果
    **/
    //$time = strtotime($str) - $timezone * 3600;
    $time = strtotime($str);
    return $time;
}

/**
 * 取得优惠活动信息
 * @param   int     $act_id     活动id
 * @return  array
 */
function favourable_info($act_id){
	
	$CommmonModel		=	new CommonModel();	
    $sql 				= 	"SELECT * FROM " . 'ybr_favourable_activity' .
            				" WHERE act_id = '$act_id'";
    $row 				= 	$CommmonModel->getRow($sql);
    
    if (!empty($row)){
        $row['start_time'] 			= 	local_date($GLOBALS['_CFG']['time_format'], $row['start_time']);
        $row['end_time'] 			= 	local_date($GLOBALS['_CFG']['time_format'], $row['end_time']);
        $row['formated_min_amount'] = 	price_format($row['min_amount']);
        $row['formated_max_amount'] = 	price_format($row['max_amount']);
        $row['gift'] 				= 	unserialize($row['gift']);
        if ($row['act_type'] == FAT_GOODS){
            $row['act_type_ext'] 	= 	round($row['act_type_ext']);
        }
    }

    return $row;
}



